#! /usr/bin/python

# Bird_upYear.py

import wx
import sqlite3 as lite

ID_DEPTH = 1

class BirdUpEdit(wx.Dialog):
    """Select past entries to edit the content of the database"""
    def __init__(self, parent, id, db, tb, title='Edit Entries'):
        wx.Dialog.__init__(self, parent, id, title, size=(665, 500))
        
        # for sqlite3 connection
        self.dbase_ = db
        self.table_ = tb
        
        self.choices_list = ['Press a button for choices']
        
        # Set the fonts
        self.dtitleFont = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        self.dtitleFont.SetPointSize(18)
        self.dlicenceFont = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        self.dlicenceFont.SetPointSize(9)
        
        # Start working on the gui framework
        self.dpanel = wx.Panel(self, -1)
        dvbox = wx.BoxSizer(wx.VERTICAL)
        
        # First level - Title
        dhbox1 = wx.BoxSizer(wx.HORIZONTAL)        
        self.dtitle = "Blupy Bird List Uploader 0.1a"
        self.dtitle_txt = wx.StaticText(self.dpanel, -1, self.dtitle)
        self.dtitle_txt.SetFont(self.dtitleFont)
        dhbox1.Add(self.dtitle_txt, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 8)
        dvbox.Add(dhbox1, 0, wx.ALIGN_CENTER)
        
        # Breathing room
        dvbox.Add((-1, 25))        
        
        #Choose which values Buttons
        
        ch_post_box_buttons = wx.BoxSizer(wx.HORIZONTAL)       
        ch_cn_button = wx.Button(self.dpanel, -1, "Common Name", size=(160, 30))
        ch_post_box_buttons.Add(ch_cn_button, 0)
        ch_sn_button = wx.Button(self.dpanel, -1, "Scientif Name", size=(160, 30))
        ch_post_box_buttons.Add(ch_sn_button, 0)
        ch_place_button = wx.Button(self.dpanel, -1, "Place", size=(160, 30))
        ch_post_box_buttons.Add(ch_place_button, 0)
        ch_notes_button = wx.Button(self.dpanel, -1, "Notes", size=(160, 30))
        ch_post_box_buttons.Add(ch_notes_button, 0)
        # Note: I don't want to add date, because I think it unlikely
        # to be the way to find what I (user?) would want to edit.
        dvbox.Add(ch_post_box_buttons, 0, wx.ALIGN_RIGHT | wx.RIGHT, 10)
        self.Bind(wx.EVT_BUTTON, self.ChooseCN, id=ch_cn_button.GetId())
        self.Bind(wx.EVT_BUTTON, self.ChooseSN, id=ch_sn_button.GetId())
        self.Bind(wx.EVT_BUTTON, self.ChoosePLACE, id=ch_place_button.GetId())
        self.Bind(wx.EVT_BUTTON, self.ChooseNOTES, id=ch_notes_button.GetId())
        
        # Breathing room
        dvbox.Add((-1, 25))        
        
        # Choose which values Drop downs
        dhbox2 = wx.BoxSizer(wx.HORIZONTAL)    
        self.choose = wx.StaticText(self.dpanel, -1, "Choose  ",  style=wx.EXPAND)
        self.choose.SetFont(self.dlicenceFont)
        dhbox2.Add(self.choose, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 8)
        self.chooser = wx.ListBox(self.dpanel, -1, choices=self.choices_list, size=(470, 300))
        self.chooser.SetFont(self.dlicenceFont)
        dhbox2.Add(self.chooser, 0, wx.EXPAND)
        dvbox.Add(dhbox2, 0, wx.ALIGN_CENTER)
        self.Bind(wx.EVT_LISTBOX, self.ChooseAll, id=self.chooser.GetId())
        try:
            # To get it to go all the way if the list item is double clicked
            self.Bind(wx.EVT_LISTBOX_DCLICK, self.allTheWay, id=self.chooser.GetId())
        except AttributeError:
            print "not"
        
        # Breathing room
        dvbox.Add((-1, 25))        
        
        #Buttons
        dhbox_buttons = wx.BoxSizer(wx.HORIZONTAL)       
        dexit_button = wx.Button(self.dpanel, -1, "Okay", size=(70, 30))
        dhbox_buttons.Add(dexit_button, 0)
        dvbox.Add(dhbox_buttons, 0, wx.ALIGN_RIGHT | wx.RIGHT, 10)        
        self.Bind(wx.EVT_BUTTON, self.OnOkay, id=dexit_button.GetId())        
        
        # Place my panel
        self.dpanel.SetSizer(dvbox)        
        
        # End of the structure part of the module
        
    # Note, all these methods are probably longer than they need be
    # I am sure that later the whole lot can be put into one method.
    
    def allTheWay(self, event):
        """Very simple, if the user has double clicked
        
        on an item in the wx.Listbox, they can go all the way
        through to the edit page without pressing the button.
        
        """
        if len(self.passIt) == 1:
            self.passIt.append(self.chooser.GetStringSelection())
        else:
            self.passIt[1] = self.chooser.GetStringSelection()
        self.OnOkay(event)
        
    def ChooseAll(self, event):
        if len(self.passIt) == 1:
            self.passIt.append(self.chooser.GetStringSelection())
        else:
            self.passIt[1] = self.chooser.GetStringSelection()
        
    def ChooseCN(self, event):
        self.passIt = ['cn']
        self.choices_list = []
        con = lite.connect(self.dbase_)
        cur = con.cursor()
        cur.execute('select cn from %s order by key desc;' % self.table_)
        self.choices_ = cur.fetchall()
        cur.close()
        con.close()
        for i in self.choices_:
            self.choices_list.append(i[0])
        self.chooser.Set(self.choices_list)
        self.chooser.SetSelection(0)
        
    def ChooseSN(self, event):
        self.passIt = ['sn']
        self.choices_list = []
        con = lite.connect(self.dbase_)
        cur = con.cursor()
        cur.execute('select sn from %s order by key desc;' % self.table_)
        self.choices_ = cur.fetchall()
        cur.close()
        con.close()
        for i in self.choices_:
            self.choices_list.append(i[0])
        self.chooser.Set(self.choices_list)
        self.chooser.SetSelection(0)        
        
    def ChoosePLACE(self, event):
        self.passIt = ['place']
        self.choices_list = []
        con = lite.connect(self.dbase_)
        cur = con.cursor()
        cur.execute('select place from %s order by key desc;' % self.table_)
        self.choices_ = cur.fetchall()
        cur.close()
        con.close()
        for i in self.choices_:
            self.choices_list.append(i[0])
        self.chooser.Set(self.choices_list)
        self.chooser.SetSelection(0)
        
    def ChooseNOTES(self, event):
        self.passIt = ['notes']
        self.choices_list = []
        con = lite.connect(self.dbase_)
        cur = con.cursor()
        cur.execute('select notes from %s order by key desc;' % self.table_)
        self.choices_ = cur.fetchall()
        cur.close()
        con.close()
        for i in self.choices_:
            self.choices_list.append(i[0])
        self.chooser.Set(self.choices_list)
        self.chooser.SetSelection(0)
      
    def OnOkay(self, event):
        """Close dialog"""
        try:
            if len(self.passIt) == 2:
                self.passback = self.passIt
                self.Close()
            else:
                self.Close()
        except:
            print "Nothing was selected in Edit mode"
            self.Close()
        
# End module